package com.lzs.system.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lzs.entity.Dept;
import com.lzs.entity.Warehouse;
import com.lzs.system.service.DeptService;
import com.lzs.system.service.WarehouseService;
import com.lzs.util.R;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 部门表 前端控制器
 * </p>
 *
 * @author 刘中帅
 * @since 2020-11-24
 */
@RestController
@RequestMapping("/system/dept")
@ApiModel(value = "部门管理")
public class DeptController {
    @Autowired
    private DeptService deptService;
    @Autowired
    private WarehouseService warehouseService;
    @GetMapping("/selectDeptTree")
    @ApiOperation(value = "部门树方法")
    public R selectDeptTree(){
        List<Integer> list=new ArrayList<>();
        list.add(25);
        R data = R.ok().data("deptTree", deptService.selectDeptTree()).data("extendNode", list);
        return data;
    }
    @PostMapping("/selectAllDeptPage/{currentpage}/{pagesize}")
    @ApiOperation(value = "部门分页查询")
    public Page<Dept> selectDeptAll(@PathVariable("currentpage") Integer page, @PathVariable("pagesize") Integer limit, @RequestBody Dept dept){
        Page<Dept> deptPage = deptService.selectAllDeptPage(page, limit,dept );
        return deptPage;
    }

    @GetMapping("/findByDeptId/{deptId}")
    @ApiOperation(value = "部门回显")
    public R findByDeptId(@PathVariable Long deptId){
        return deptService.findByDeptId(deptId);
    }
    // 新增修改
    @PostMapping("/insertOrUpdateDept")
    @ApiOperation(value = "部门新增修改")
    public  R insertOrUpdateDept(@RequestBody  Dept dept){
        return deptService.insertOrUpdateDept(dept);
    }
    // 根据Id删除
    @DeleteMapping("/deleteDept/{deptId}")
    @ApiOperation(value = "根据部门Id删除")
    public R deleteDept(@PathVariable Long deptId){
        boolean b = deptService.removeById(deptId);
        if (b){
            return R.ok().message("删除成功");
        }
        return R.error().message("删除失败");
    }
    @GetMapping("/selectAllWarehouse/{deptId}")
    @ApiOperation(value = "查询所有仓库")
    public R selectAllWarehouse(@PathVariable Long deptId){
        List<Warehouse> warehouses = warehouseService.list();
        List<Long> warehouseIds = deptService.selectWarehouseByDeptId(deptId);
        R r = R.ok().data("warehouses", warehouses).data("warehouseIds", warehouseIds);
        return  r;
    }

    // 改变当前部门仓库
    @PostMapping("/updateWarehouseByDeptId/{deptId}")
    @ApiOperation(value = "部门分配仓库")
    public R updateWarehouseByDeptId(@PathVariable Long deptId ,@RequestBody List<Long> warehouseIds ){
        R r = deptService.updateWarehouseByDeptId(deptId ,warehouseIds);
        return r;
    }
}

